package com.desheng.Service;


import com.desheng.bean.Item;
import com.desheng.bean.ItemExample;
import com.desheng.bean.User;
import com.desheng.dao.ItemMapper;
import com.desheng.util.MybatisUtil;
import com.easyFramework.annotation.Service;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;

/**
 * @author yezl
 * @date 2018/6/1 22:41
 * @decription this is :根据bookname author press kind other
 */
@Service
public class BookService{

//    @Cacheable("searchbook") //标注该方法查询的结果进入缓存，再次访问时直接读取缓存中的数据
//    public List<Item> searchBook(String keywords,long userid){
//        System.out.println("你搜索的是"+keywords);
//        String[] keyword= keywords.split("");
//        HashMap<Integer, Item> searchlist =new HashMap<Integer, Item>();
//        for(int i=0;i<keyword.length;i++){
//            keyword[i]="%"+keyword[i]+"%";
//        }
//        ItemExample ie =new ItemExample();
//
//        for(String kw:keyword){
//            ItemExample.Criteria iec = ie.or().andAuthorLike(kw).andStatusEqualTo(0).andSelleridNotEqualTo(userid).;
//            ItemExample.Criteria iec1 = ie.or().andBooknameLike(kw).andStatusEqualTo(0).andSelleridNotEqualTo(userid);
//            ItemExample.Criteria iec2 = ie.or().andPressLike(kw).andStatusEqualTo(0).andSelleridNotEqualTo(userid);
//            ItemExample.Criteria iec3 = ie.or().andKindLike(kw).andStatusEqualTo(0).andSelleridNotEqualTo(userid);
//            ie.or(iec);
//            ie.or(iec1);
//            ie.or(iec2);
//            ie.or(iec3);
//        }
//        List<Item> itemlist = itemMapper.selectByExample(ie);
//        return itemlist;
//    }

    public List<Item> searchBook(String keywords, long userid, int start, int num, String orderStr){
        System.out.println("你搜索的是"+keywords);

        SqlSession session = MybatisUtil.openSession();
        ItemMapper itemMapper = session.getMapper(ItemMapper.class);

        String[] keyword= keywords.split("");
        HashMap<Integer, Item> searchlist =new HashMap<Integer, Item>();
        for(int i=0;i<keyword.length;i++){
            keyword[i]="%"+keyword[i]+"%";
            System.out.println(keyword[i]);
        }
        ItemExample ie =new ItemExample();
        ie.setOrderByClause(orderStr);
        ie.setStart(start);
        ie.setLimit(num);

        for(String kw:keyword){
            ItemExample.Criteria iec = ie.or().andAuthorLike(kw).andStatusEqualTo(0).andSelleridNotEqualTo(userid);
            ItemExample.Criteria iec1 = ie.or().andBooknameLike(kw).andStatusEqualTo(0).andSelleridNotEqualTo(userid);
            ItemExample.Criteria iec2 = ie.or().andPressLike(kw).andStatusEqualTo(0).andSelleridNotEqualTo(userid);
            ItemExample.Criteria iec3 = ie.or().andKindLike(kw).andStatusEqualTo(0).andSelleridNotEqualTo(userid);
            ie.or(iec);
            ie.or(iec1);
            ie.or(iec2);
            ie.or(iec3);
        }
        session.commit();
        List<Item> itemlist = itemMapper.selectByExample(ie);
        session.close();
        return itemlist;
    }

    public boolean addBook(Item item, User user){
        SqlSession session = MybatisUtil.openSession();
        ItemMapper itemMapper = session.getMapper(ItemMapper.class);
        ItemExample ie =new ItemExample();
        ItemExample.Criteria iec =ie.createCriteria();
        iec.andBooknameEqualTo(item.getBookname());
        iec.andSelleridEqualTo(item.getSellerid());
        List<Item> itemlist=itemMapper.selectByExample(ie);
        if(itemlist.size()>0)
            return false;
        item.setStatus(0);
        itemMapper.insert(item);
        session.commit();
        session.close();
        return true;
    }



    public Item getBook(int itemid){
        SqlSession session = MybatisUtil.openSession();
        ItemMapper itemMapper = session.getMapper(ItemMapper.class);
        ItemExample ie =new ItemExample();
        ItemExample.Criteria iec =ie.createCriteria();
        Item it=itemMapper.selectByPrimaryKey(itemid);
//        if(it.getStatus()!=0){
////            return
////        }
        //session.commit();
        session.close();
        return it;
    }

    public Item getItem(int itemid){
        SqlSession session = MybatisUtil.openSession();
        ItemMapper itemMapper = session.getMapper(ItemMapper.class);
        Item item= itemMapper.selectByPrimaryKey(itemid);
        //session.commit();
        session.close();
        if(item.getStatus()==0){
            return item;
        }
        return null;
    }

    public List<Item> getallBook(long userid,int start,int num,String orderstr){
        SqlSession session = MybatisUtil.openSession();
        ItemMapper itemMapper = session.getMapper(ItemMapper.class);
        ItemExample ie =new ItemExample();
        ItemExample.Criteria iec = ie.createCriteria();
        iec.andStatusEqualTo(0).andSelleridNotEqualTo(userid);
        ie.setOrderByClause(orderstr);
        ie.setStart(start);
        ie.setLimit(num);
        List<Item> itemlist = itemMapper.selectByExample(ie);
        //session.commit();
        session.close();
        return itemlist;
    }



}
